//======================================================================================
//  Notes:Use this command to anonymously check out the latest project source code:
//  Non-members may check out a read-only working copy anonymously over HTTP.
//  http://YHQCodingWsp.googlecode.com/svn/trunk/
//
//  Author:		Kevin Yang
//  Email:		Bigearrabbit@qq.com
//
//  DATE:		2013-4-22
//
//  File Name:	YHQFile.cpp
//
//  Notes:
//
//======================================================================================

#include "YHQFile.h"

//Qt Library
#include <QDir>

#include <QTextStream>

//-----------------------------------------------------
YHQFile::YHQFile()
{
}
//-----------------------------------------------------
YHQFile::~YHQFile()
{
}
//-----------------------------------------------------
bool YHQFile::DeleteFolderContents(const QString &folderDir)
{
    QDir dir(folderDir);
    QFileInfoList fileList;
    QFileInfo curFile;
    if(!dir.exists())  {return false;}//文件不存，则返回false
    fileList=dir.entryInfoList(QDir::Dirs|QDir::Files
                               |QDir::Readable|QDir::Writable
                               |QDir::Hidden|QDir::NoDotAndDotDot
                               ,QDir::Name);
    while(fileList.size()>0)//跳出条件
    {
        int infoNum=fileList.size();
        for(int i=infoNum-1;i>=0;i--)
        {
            curFile=fileList[i];
            if(curFile.isFile())//如果是文件，删除文件
            {
                QFile fileTemp(curFile.filePath());
                fileTemp.remove();
                fileList.removeAt(i);
            }
            if(curFile.isDir())//如果是文件夹
            {
                QDir dirTemp(curFile.filePath());
                QFileInfoList fileList1=dirTemp.entryInfoList(QDir::Dirs|QDir::Files
                                                              |QDir::Readable|QDir::Writable
                                                              |QDir::Hidden|QDir::NoDotAndDotDot
                                                              ,QDir::Name);
                if(fileList1.size()==0)//下层没有文件或文件夹
                {
                    dirTemp.rmdir(".");
                    fileList.removeAt(i);
                }
                else//下层有文件夹或文件
                {
                    for(int j=0;j<fileList1.size();j++)
                    {
                        if(!(fileList.contains(fileList1[j])))
                            fileList.append(fileList1[j]);
                    }
                }
            }
        }
    }
   return true;
}
//-----------------------------------------------------
bool YHQFile::DeleteFolder(const QString &iFolderDir)
{
    DeleteFolderContents(iFolderDir);
    QDir dir;
    dir.rmdir(iFolderDir);
}
//-----------------------------------------------------
bool FindFile(const QString & path)
{
    QDir dir(path);
    if (!dir.exists())
        return false;
    dir.setFilter(QDir::Dirs|QDir::Files);
    dir.setSorting(QDir::DirsFirst);
    QFileInfoList list = dir.entryInfoList();
    int i=0;
    do{

        QFileInfo fileInfo = list.at(i);
        if((fileInfo.fileName()==".") | (fileInfo.fileName()==".."))
        {
            i++;
            continue;
        }
        bool bisDir=fileInfo.isDir();
        if(bisDir)
        {
            FindFile(fileInfo.filePath());
        }
        else{
         }
        i++;
    }while(i<list.size());
    return true;
}
//------------------------------------------
bool FindFolder(const QString & path)
{
    QDir dir(path);
    if (!dir.exists())
        return false;
    dir.setFilter(QDir::Dirs);
    QFileInfoList list = dir.entryInfoList();

    const int size = list.size();
    for (int i=0;i<size;i++)
    {

        QFileInfo fileInfo = list.at(i);
        if((fileInfo.fileName()==".")|(fileInfo.fileName()==".."))
        {
            continue;
        }

    }//for i
    return true;
}
//----------------------------------------------------
bool YHQFile::WriteToTextFile(const QString & iPath, const QStringList &iList)
{
    if(iList.size() == 0) return false;
    QFile file(iPath);

    if(!file.open(QIODevice::WriteOnly))
    {
        return false;
    }

    QTextStream fileOut(&file);
    fileOut.setCodec("ANSI");  // ANSI

    for(int i=0;i<iList.size();i++)
    {
        fileOut <<iList[i]<<endl;
    }

    file.flush();

    file.close();
}
